/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */
package cn.yzc.autoconfig;

import cn.yzc.interceptor.LoginInterceptor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * <p>Project: SpringBootPro - LoginInterceptorAutoConfig</p>
 * <p>Powered by yzc On 2023-11-09 14:27:39</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@AutoConfiguration //标识自动配置类
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableConfigurationProperties(LoginInterceptorProperties.class)
@ConditionalOnClass(HandlerInterceptor.class)
@ConditionalOnProperty(prefix = "spring.mvc.auth",value = "enabled",havingValue = "true",matchIfMissing = true)
public class LoginInterceptorAutoConfig implements WebMvcConfigurer {

    LoginInterceptorProperties properties;

    /**
     * 构造注入
     * @param properties
     */
    public LoginInterceptorAutoConfig (LoginInterceptorProperties properties){
        this.properties = properties;
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(properties.getPathPatterns())
                .excludePathPatterns(properties.getExcludePathPatterns());
        WebMvcConfigurer.super.addInterceptors(registry);
    }
}
